<?php

require_once 'classes/LightPressPlugin.php';

class PostGenericAction extends LightPressPlugin {

    var $default_context = LP_CONTEXT_POST;
    var $description = 'Base class for post plugins';

    var $active = false;
    var $hooks = array('parse_post');
    
    var $_message;
    var $_message_class;
    var $_action;
    var $_user_data;
    var $_labels;
    var $_remember;

    function PostGenericAction(&$frontend, $args=array(), $dummy_run=false) {
        
        $this->LightPressPlugin($frontend, $args, $dummy_run);

        if (!$dummy_run) {
            
            if (isset($_REQUEST['action']))                                 // WPFF-style
                $this->_action = $_REQUEST['action'];
            else if (isset($_REQUEST['tb']) && ($_REQUEST['tb'] == '1'))    // WP-style
                $this->_action = 'trackback';
            else
                $this->_action = null;
        }
        
    }
    
    function hide() {
        $this->_frontend->tpl->setVar('PLUGIN_' . strtoupper(get_class($this)), '');
    }
    
    function getDescription() {
        return $this->description;
    }

    function _bootstrap(&$post, $default_message,
                        $get_and_post=false, $use_cookies=true,
                        $post_fields=array('name', 'email', 'url', 'remember', 'text', 'recipients')) {
        $tpl =& $this->_frontend->tpl;
        $messages =& $this->_frontend->messages;
        $options =& $this->_frontend->options;

        // retrieve the message or set it to a sane default
        if (isset($_GET['message']) && !empty($_GET['message'])) {
            $this->_message = get_magic_quotes_gpc() ? stripslashes($_GET['message']) : $_GET['message'];
            $this->_message_class = 'comment_form_warning';
        } else {
            $this->_message = $messages['comment_form_message'];
            $this->_message_class = 'comment_form_message';
        }
        
        // set up identity variables from cookie, if there is one
        if ($use_cookies && isset($_COOKIE['lp_identity'])) {
            $user_data = get_magic_quotes_gpc() ? stripslashes($_COOKIE['lp_identity']) : $_COOKIE['lp_identity'];
            $user_data = @unserialize($user_data);
            if (!is_array($user_data))
                $user_data = array();
        } else {
            $user_data = array();
        }
        
        // retrieve form variables
        $labels = array();
        
        foreach ($post_fields as $key) {
            if (isset($_POST[$key]))
                $value = get_magic_quotes_gpc() ? stripslashes(trim($_POST[$key])) : trim($_POST[$key]);
            else if ($get_and_post && isset($_GET[$key]))
                $value = get_magic_quotes_gpc() ? stripslashes(trim($_GET[$key])) : trim($_GET[$key]);
            else if (isset($user_data["ud_$key"]))
                $value = $user_data["ud_$key"];
            else
                $value = '';
            $value = str_replace('{', '&#123;', $value);
            $value = str_replace('}', '&#125;', $value);
            if ($key == 'text')
                $user_data["ud_$key"] = strip_tags($value, $options['allowed_tags']);
            else
                $user_data["ud_$key"] = strip_tags($value);
            $user_data["ud_label_$key"] = '';
        }
        
        // store user details
        // the plugin extending this (eg PostComment) is responsible for removing
        // the cookie, as it knows if we are reacting to a form submission
        if ($use_cookies && isset($_REQUEST['remember']) && $_REQUEST['remember'] == '1')
            $this->_frontend->setUserCookie($user_data);
        
        $this->_user_data =& $user_data;
        $this->_labels =& $labels;
    }

}

?>